package ar.com.ctrln.view.render.compras.insumo;

import java.util.HashMap;
import java.util.Map;

import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zul.Hbox;
import org.zkoss.zul.Image;
import org.zkoss.zul.Label;
import org.zkoss.zul.Row;
import org.zkoss.zul.RowRenderer;
import org.zkoss.zul.Vlayout;

import ar.com.ctrln.persistence.model.insumo.Insumo;

public class InsumoRowRender implements RowRenderer<Insumo> {

	@SuppressWarnings("unchecked")
	@Override
	public void render(Row row, Insumo data,int arg) throws Exception {

		Vlayout stock = new Vlayout();
		row.appendChild(new Label(data.getTipoInsumo()));
    	row.appendChild(new Label(data.getNombre()));
    	Label cant = new Label (String.valueOf(data.getStock()));
		Label uni = new Label(data.getUnidadMedida());
		
		uni.setStyle("font-size:10px;");
		cant.setParent(stock);
		uni.setParent(stock);
		stock.setParent(row);
    	   	
    	Hbox box = new Hbox();
        box.setSpacing("12px");
        box.setWidth("100%");
        box.setPack("center");
        Image img1 = new Image();
        Image img2 = new Image();
        img1.setSrc("/resources/icons/verTabla.png");
		img2.setSrc("/resources/icons/cart.png");
        img1.setTooltiptext("Ver detalles del insumo");
        img2.setTooltiptext("Realizar pedido de compra del insumo");
	    img1.setParent(box);
        img2.setParent(box);
        img1.setWidth("32px");
        img1.setHeight("32px");
        img2.setWidth("32px");
        img2.setHeight("32px");
        box.setParent(row);
        row.setTooltiptext(data.getDescripcion());
		row.setAttribute("data", data);
	
		img1.addEventListener("onClick", new VerInsumoListener());
		img2.addEventListener("onClick", new PedidoInsumoListener());
	}
}

@SuppressWarnings("rawtypes")
class VerInsumoListener implements org.zkoss.zk.ui.event.EventListener {

	@Override
	public void onEvent(Event evt) throws Exception {

		Insumo insumo = (Insumo) ((Row) evt.getTarget().getParent().getParent()).getAttribute("data");
		Map<String, Insumo> args = new HashMap<String, Insumo>();
		args.put("data", insumo);
		Executions.createComponents("/zul/compras/insumos/registrarInsumo.zul",null
				, args);
	}
}

@SuppressWarnings("rawtypes")
class PedidoInsumoListener implements org.zkoss.zk.ui.event.EventListener {

	@Override
	public void onEvent(Event evt) throws Exception {

		Insumo insumo = (Insumo) ((Row) evt.getTarget().getParent().getParent()).getAttribute("data");
		Map<String, Insumo> args = new HashMap<String, Insumo>();
		args.put("data", insumo);
		Executions.createComponents("/zul/compras/registrarNotaPedido.zul",null
				, args);
	}
}
